#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int fd1;
    fd1 = open("output.txt", O_RDWR);

    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第一次读取
    char buf[3];
    int res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第二次读取
    memset(buf, 0, sizeof(buf));
    res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第三次读取
    memset(buf, 0, sizeof(buf));
    res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第四次读取
    lseek(fd1, 2, SEEK_CUR);
    memset(buf, 0, sizeof(buf));
    res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第五次读取   偏移到文件开头
    lseek(fd1, 0, SEEK_SET);
    memset(buf, 0, sizeof(buf));
    res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    // 第六次读取   偏移到文件末尾
    lseek(fd1, -2, SEEK_END);
    memset(buf, 0, sizeof(buf));
    res_read = read(fd1, buf, 2);
    printf("1read %d bytes ,buf = %s\n", res_read, buf);
    printf("fd1 = %ld\n", lseek(fd1, 0, SEEK_CUR));

    close(fd1);
    return 0;
}
